package com.ebt.m.proposal_v2;

import android.text.TextUtils;
import android.util.Log;

import com.ebt.m.proposal_v2.bean.ApplicantEntity;
import com.ebt.m.proposal_v2.bean.InsuranceEntity;
import com.ebt.m.proposal_v2.bean.InsuranceOption;
import com.ebt.m.proposal_v2.bean.InsuredEntity;
import com.ebt.m.proposal_v2.bean.ProposalEntity;
import com.ebt.m.proposal_v2.dao.response.BusinessConfig;
import com.ebt.m.utils.JsonUtil;

import java.util.List;

/**
 * Created by Rick.Wang on 2017/12/1.
 */

public class EbtEqualUtil {

    public static boolean stringEquals(String a, String b){
        if (a == null){
            if (b == null){
                return true;
            }else{
                if (TextUtils.isEmpty(b.trim())){
                    return true;
                }else{
                    return false;
                }
            }
        }else{
            if (b == null){
                if (TextUtils.isEmpty(a.trim())){
                    return true;
                }else{
                    return false;
                }
            }else{
                return a.trim().equals(b.trim());
            }
        }
    }

    public static boolean doubleEquals(double a, double b) {
        if ((a- b> -0.000001) && (a- b) < 0.000001)
            return true;
        else
            return false;
    }

    public static boolean isProposalEntityEmpty(ProposalEntity a){
        if (a == null){
            return true;
        }else{
            if (isTextEmpty(a.pickNum)
                    && isTextEmpty(a.proposalName)
                    && isTextEmpty(a.description)
                    && isTextEmpty(a.thumbnail)
                    && doubleEquals(a.totalCoverage, 0)
                    && doubleEquals(a.totalPremium, 0)
                    && isApplicantEntityEmpty(a.applicant)
                    && a.shouldSaveClient == 0
                    && isBusinessConfigEmpty(a.businessConfig)
                    && isListEmpty(a.insured)){
                return true;
            }else{
                return false;
            }
        }
    }

    public static boolean proposalEntityEquals(ProposalEntity a, ProposalEntity b){
        Log.d("wangwang","proposalEntityEquals");
        if (isProposalEntityEmpty(a)){
            Log.d("wangwang","proposalEntityEquals 1");
            if (isProposalEntityEmpty(b)){
                Log.d("wangwang","proposalEntityEquals 11 ");
                return true;
            }else{
                return false;
            }
        }else{
            Log.d("wangwang","proposalEntityEquals 2");
            if (isProposalEntityEmpty(b)){
                Log.d("wangwang","proposalEntityEquals 22");
                return false;
            }else{
//                Log.d("wangwang","proposalEntityEquals 23");
                Log.d("wangwang","proposalEntityEquals pickNUmber " + a.pickNum + " b.picknumber : " + b.pickNum);
                if (stringEquals(a.pickNum, b.pickNum)){
                    Log.d("wangwang","proposalEntityEquals pickNUmber yes");
                }else{
                    Log.d("wangwang","proposalEntityEquals pickNUmber no");
                }
                Log.d("wangwang","proposalEntityEquals proposalName " + a.proposalName + " b.proposalName : " + b.proposalName);
                if (stringEquals(a.proposalName, b.proposalName)){
                    Log.d("wangwang","proposalEntityEquals proposalName yes");
                }else{
                    Log.d("wangwang","proposalEntityEquals proposalName no");
                }
                Log.d("wangwang","proposalEntityEquals description " + a.description + " b.description : " + b.description);

                if (stringEquals(a.description, b.description)){
                    Log.d("wangwang","proposalEntityEquals description yes");
                }else{
                    Log.d("wangwang","proposalEntityEquals description no");
                }

                Log.d("wangwang","proposalEntityEquals thumbnail " + a.thumbnail + " b.thumbnail : " + b.thumbnail);
                if (stringEquals(a.thumbnail, b.thumbnail)){
                    Log.d("wangwang","proposalEntityEquals thumbnail yes");
                }else{
                    Log.d("wangwang","proposalEntityEquals thumbnail no");
                }
                Log.d("wangwang","proposalEntityEquals totalPremium " + a.totalPremium + " b.totalPremium : " + b.totalPremium);
                if (doubleEquals(a.totalPremium, b.totalPremium)){
                    Log.d("wangwang","proposalEntityEquals totalPremium yes");
                }else{
                    Log.d("wangwang","proposalEntityEquals totalPremium no");
                }
                Log.d("wangwang","proposalEntityEquals totalCoverage " + a.totalCoverage + " b.totalCoverage : " + b.totalCoverage);
                if (doubleEquals(a.totalCoverage, b.totalCoverage)){
                    Log.d("wangwang","proposalEntityEquals totalCoverage yes");
                }else{
                    Log.d("wangwang","proposalEntityEquals totalCoverage no");
                }

                if (applicantEntityEquals(a.applicant, b.applicant)){
                    Log.d("wangwang","proposalEntityEquals applicant yes");
                }else{
                    Log.d("wangwang","proposalEntityEquals applicant no");
                }

                Log.d("wangwang","proposalEntityEquals shouldSaveClient " + a.shouldSaveClient + " b.shouldSaveClient : " + b.shouldSaveClient);
                if (a.shouldSaveClient == b.shouldSaveClient){
                    Log.d("wangwang","proposalEntityEquals shouldSaveClient yes");
                }else{
                    Log.d("wangwang","proposalEntityEquals shouldSaveClient no");
                }

                if (businessConfigEquals(a.businessConfig, b.businessConfig)){
                    Log.d("wangwang","proposalEntityEquals businessConfig yes");
                }else{
                    Log.d("wangwang","proposalEntityEquals businessConfig no");
                }

                if (insuredEntityListEquals(a.insured, b.insured)){
                    Log.d("wangwang","proposalEntityEquals insured yes");
                }else{
                    Log.d("wangwang","proposalEntityEquals insured no");
                }

                if (stringEquals(a.pickNum, b.pickNum)
                        && stringEquals(a.proposalName, b.proposalName)
                        && stringEquals(a.description, b.description)
                        && stringEquals(a.thumbnail, b.thumbnail)
                        && doubleEquals(a.totalPremium, b.totalPremium)
                        && doubleEquals(a.totalCoverage, b.totalCoverage)
                        && applicantEntityEquals(a.applicant, b.applicant)
                        && a.shouldSaveClient == b.shouldSaveClient
                        && businessConfigEquals(a.businessConfig, b.businessConfig)
                        && insuredEntityListEquals(a.insured, b.insured)){
                    Log.d("wangwang","proposalEntityEquals 24");
                    return true;
                }else{
                    Log.d("wangwang","proposalEntityEquals 25");
                    return false;
                }
            }
        }
    }

    public static boolean insuredEntityListEquals(List<InsuredEntity> a, List<InsuredEntity> b){
        if (a == null || a.size() == 0){
            Log.d("wangwang","insuredEntityListEquals 1");
            if (b == null || b.size() == 0){
                Log.d("wangwang","insuredEntityListEquals 2");
                return true;
            }else{
                Log.d("wangwang","insuredEntityListEquals 3");
                return false;
            }
        }else{
            Log.d("wangwang","insuredEntityListEquals 4");
            if (b == null || b.size() == 0){
                Log.d("wangwang","insuredEntityListEquals 5");
                return false;
            }else{
                Log.d("wangwang","insuredEntityListEquals 6 a.size() : " + a.size() + " b.size() : " + b.size());
                if (a.size() != b.size()){
                    return false;
                }else{
                    Log.d("wangwang","insuredEntityListEquals 7");
                    int size = a.size();
                    for(int i = 0; i < size; i++){
                        if (insuredEntityEquals(a.get(i), b.get(i))){
                            Log.d("wangwang","insuredEntityListEquals 8");
                            continue;
                        }else{
                            Log.d("wangwang","insuredEntityListEquals 999 ");
                            Log.d("wangwang","insuredEntityListEquals a.get(i) : " + JsonUtil.parseObjectToJson(a.get(i)));
                            Log.d("wangwang","insuredEntityListEquals b.get(i) : " + JsonUtil.parseObjectToJson(b.get(i)));
                            return false;
                        }
                    }
                    return true;
                }
            }
        }
    }

    public static boolean isInsuredEntityEmpty(InsuredEntity a){
        if (a == null){
            return true;
        }else{
            if (isTextEmpty(a.clientAccountId)
                    && isTextEmpty(a.name)
                    && isTextEmpty(a.birthday)
                    && (a.sex == 0)
                    && (a.age == 0)
                    && (a.profession == 0)
                    && (a.relation == 0)
                    && (a.risk == null || a.risk.size() == 0)){
                return true;
            }else{
                return false;
            }
        }
    }

    public static boolean insuredEntityEquals(InsuredEntity a, InsuredEntity b){
        if (isInsuredEntityEmpty(a)){
            if (isInsuredEntityEmpty(b)){
                return true;
            }else{
                Log.d("wangwang","--------- insuredEntityEquals 1");
                return false;
            }
        }else{
            if (isInsuredEntityEmpty(b)){
                Log.d("wangwang","--------- insuredEntityEquals 2");
                return false;
            }else{
                if (stringEquals(a.clientAccountId, b.clientAccountId)){
                    Log.d("wangwang","--------- insuredEntityEquals 3");
                }else{
                    Log.d("wangwang","--------- insuredEntityEquals 4");
                }

                if (stringEquals(a.name, b.name)){
                    Log.d("wangwang","--------- insuredEntityEquals 5");
                }else{
                    Log.d("wangwang","--------- insuredEntityEquals 6");
                }

                if (stringEquals(a.birthday, b.birthday)){
                    Log.d("wangwang","--------- insuredEntityEquals 7");
                }else{
                    Log.d("wangwang","--------- insuredEntityEquals 8");
                }

                if ((a.sex == b.sex)){
                    Log.d("wangwang","--------- insuredEntityEquals 9");
                }else{
                    Log.d("wangwang","--------- insuredEntityEquals 10");
                }

                if ((a.age == b.age)){
                    Log.d("wangwang","--------- insuredEntityEquals 11");
                }else{
                    Log.d("wangwang","--------- insuredEntityEquals 12");
                }

                if ((a.profession == b.profession)){
                    Log.d("wangwang","--------- insuredEntityEquals 13");
                }else{
                    Log.d("wangwang","--------- insuredEntityEquals 14");
                }

                if ((a.relation == b.relation)){
                    Log.d("wangwang","--------- insuredEntityEquals 15");
                }else{
                    Log.d("wangwang","--------- insuredEntityEquals 16");
                }
                if (listInsuranceEntityEquals(a.risk, b.risk)){
                    Log.d("wangwang","--------- insuredEntityEquals 17");
                }else{
                    Log.d("wangwang","--------- insuredEntityEquals 18");
                }

                if (stringEquals(a.clientAccountId, b.clientAccountId)
                        && stringEquals(a.name, b.name)
                        && stringEquals(a.birthday, b.birthday)
                        && (a.sex == b.sex)
                        && (a.age == b.age)
                        && (a.profession == b.profession)
                        && (a.relation == b.relation)
                        && (listInsuranceEntityEquals(a.risk, b.risk))){
                    return true;
                }else{
                    return false;
                }
            }
        }
    }

    public static boolean listInsuranceEntityEquals(List<InsuranceEntity> a, List<InsuranceEntity> b){
        if (a == null || a.size() == 0){
            if (b == null || b.size() ==0){
                return true;
            }else{
                return false;
            }
        }else{
            if (b == null || b.size() ==0){
                return false;
            }else{
                if (a.size() != b.size()){
                    return false;
                }else{
                    int size = a.size();
                    for (int i = 0; i < size; i ++){
                        if(insuranceEntityEquals(a.get(i), b.get(i))){
                            continue;
                        }else{
                            return false;
                        }
                    }
                    return true;
                }
            }
        }
    }

    //TODO 单项太多了
    public static boolean isInsuranceEntityEmpty(InsuranceEntity a){
        if (a == null){
            return true;
        }else{
            return false;
        }
    }

    public static boolean insuranceEntityEquals(InsuranceEntity a, InsuranceEntity b){
        if (isInsuranceEntityEmpty(a)){
            if (isInsuranceEntityEmpty(b)){
                return true;
            }else{
                return false;
            }
        }else{
            if (isInsuranceEntityEmpty(b)){
                return false;
            }else{
                if ((a.isValidAccordingToInsuredEntityAndInsuranceEntity == b.isValidAccordingToInsuredEntityAndInsuranceEntity)
                        && businessConfigEquals(a.businessConfig, b.businessConfig)
                        && (a.shouldSaveClient == b.shouldSaveClient)
                        && listStringEquals(a.insuredIds, b.insuredIds)
                        && (a.brandId == b.brandId)
                        && (a.productId == b.productId)
                        && (a.isMain == b.isMain)
                        && (a.sort == b.sort)
                        && (a.riskType == b.riskType)
                        && (stringEquals(a.productName, b.productName))
                        && (stringEquals(a.thumbnail, b.thumbnail))
                        && (stringEquals(a.description, b.description))
                        && (stringEquals(a.waringMessage, b.waringMessage))
                        && (stringEquals(a.planCode, b.planCode))
                        && (stringEquals(a.exemptPerson, b.exemptPerson))
                        && (stringEquals(a.accMaxAge, b.accMaxAge))
                        && (stringEquals(a.accMinAge, b.accMinAge))
                        && (stringEquals(a.accOccupation, b.accOccupation))
                        && (stringEquals(a.accSex, b.accSex))
                        && (a.isEnabled == b.isEnabled)
                        && (a.isBindingMain == b.isBindingMain)
                        && (listStringEquals(a.exemptProduct, b.exemptProduct))
                        && (insuranceOptionEquals(a.coverage, b.coverage))
                        && (insuranceOptionEquals(a.premium, b.premium))
                        && (insuranceOptionEquals(a.coveragePeriod, b.coveragePeriod))
                        && (insuranceOptionEquals(a.paymentPeriod, b.paymentPeriod))
                        && (listInsuranceOptionEquals(a.featureData, b.featureData))
                        && (listInsuranceOptionEquals(a.extraData, b.extraData))
                        ){
                    return true;
                }else{
                    return false;
                }
            }
        }
    }

    public static boolean listInsuranceOptionEquals(List<InsuranceOption> a, List<InsuranceOption> b){
        if (a == null || a.size() == 0){
            if (b == null || b.size() == 0){
                return true;
            }else{
                return false;
            }
        }else{
            if (b == null || b.size() == 0){
                return false;
            }else{
                if (a.size() != b.size()){
                    return false;
                }else{
                    int size = a.size();
                    for (int i = 0; i < size; i++){
                        if (insuranceOptionEquals(a.get(i), b.get(i))){
                            continue;
                        }else{
                            return false;
                        }
                    }
                    return true;
                }
            }
        }
    }

    //TODO
    public static boolean isInsuranceOptionEmpty(InsuranceOption a){
        if (a == null){
            return true;
        }else{
            return false;
        }
    }

    public static boolean insuranceOptionEquals(InsuranceOption a, InsuranceOption b){
        if (isInsuranceOptionEmpty(a)){
            if (isInsuranceOptionEmpty(b)){
                return true;
            }else{
                return false;
            }
        }else{
            if (isInsuranceOptionEmpty(b)){
                return false;
            }else{
                if (stringEquals(a.displayName, b.displayName)
                        && stringEquals(a.fieldName, b.fieldName)
                        && stringEquals(a.value, b.value)){
                    return true;
                }else{
                    return false;
                }
            }
        }
    }

    public static boolean businessConfigEquals(BusinessConfig a, BusinessConfig b){
        if (isBusinessConfigEmpty(a)){
            if (isBusinessConfigEmpty(b)){
                return true;
            }else{
                return false;
            }
        }else{
            if (isBusinessConfigEmpty(b)){
                return false;
            }else{
                if (a.proposal_supportMultipleInsured == b.proposal_supportMultipleInsured
                        && a.proposal_minInsured == b.proposal_minInsured
                        && a.proposal_maxInsured == b.proposal_maxInsured
                        && isProposalOtherInsuredEquals(a.proposal_other_insured, b.proposal_other_insured)){
                    return true;
                }else{
                    return false;
                }
            }
        }
    }

    public static boolean applicantEntityEquals(ApplicantEntity a, ApplicantEntity b){
        if (a == null){
            if (b == null){
                return true;
            }else{
                if (isTextEmpty(b.clientAccountId) && isTextEmpty(b.name) && isTextEmpty(b.birthday) && isTextEmpty(b.phone) && b.sex == 0 && b.age == 0){
                    return true;
                }else{
                    return false;
                }
            }
        }else{
            return (stringEquals(a.clientAccountId, b.clientAccountId)
            && stringEquals(a.name, b.name)
            && stringEquals(a.birthday, b.birthday)
            && stringEquals(a.phone, b.phone)
            && (a.sex == b.sex)
            && (a.age == b.age));
        }
    }

    public static boolean isTextEmpty(String s){
        if (s == null){
            return true;
        }
        if (s.trim().length() == 0){
            return true;
        }
        return false;
    }

    public static boolean isApplicantEntityEmpty(ApplicantEntity a){
        if (a == null){
            return true;
        }else{
            if (isTextEmpty(a.clientAccountId)
                    && isTextEmpty(a.name)
                    && isTextEmpty(a.birthday)
                    && isTextEmpty(a.phone)
                    && a.sex == 0
                    && a.age == 0){
                return true;
            }else{
                return false;
            }
        }
    }

    public static boolean isBusinessConfigEmpty(BusinessConfig a){
        if (a == null){
            return true;
        }else{
            if (a.proposal_maxInsured == 0
                    && a.proposal_minInsured == 0
                    && a.proposal_supportMultipleInsured == 0
                    && isProposalOtherInsuredEmpty(a.proposal_other_insured)){
                return true;
            }else{
                return false;
            }
        }
    }

    public static boolean isProposalOtherInsuredEquals(BusinessConfig.ProposalOtherInsured a, BusinessConfig.ProposalOtherInsured b){
        if (isProposalOtherInsuredEmpty(a)){
            if (isProposalOtherInsuredEmpty(b)){
                return true;
            }else{
                return false;
            }
        }else{
            if (isProposalOtherInsuredEmpty(b)){
                return false;
            }else{
                return (businessConfigProfessionEquals(a.profession, b.profession)
                    && businessConfigAgeEquals(a.age, b.age)
                    && businessConfigRelationEquals(a.relation, b.relation)
                    && businessConfigSexEquals(a.sex, b.sex));
            }
        }
    }

    public static boolean businessConfigAgeEquals(BusinessConfig.ProposalOtherInsured.Age a, BusinessConfig.ProposalOtherInsured.Age b){
        if (isBusinessConfigAgeEmpty(a)){
            if (isBusinessConfigAgeEmpty(b)){
                return true;
            }else{
                return false;
            }
        }else{
            if (isBusinessConfigAgeEmpty(b)){
                return false;
            }else{
                return (stringEquals(a.defaultValue, b.defaultValue) && listStringEquals(a.range, b.range));
            }
        }
    }

    public static boolean businessConfigSexEquals(BusinessConfig.ProposalOtherInsured.Sex a, BusinessConfig.ProposalOtherInsured.Sex b){
        if (isBusinessConfigSexEmpty(a)){
            if (isBusinessConfigSexEmpty(b)){
                return true;
            }else{
                return false;
            }
        }else{
            if (isBusinessConfigSexEmpty(b)){
                return false;
            }else{
                return ((a.defaultValue == b.defaultValue) && listIntegerEquals(a.list, b.list));
            }
        }
    }

    public static boolean businessConfigRelationEquals(BusinessConfig.ProposalOtherInsured.Relation a, BusinessConfig.ProposalOtherInsured.Relation b){
        if (isBusinessConfigRelationEmpty(a)){
            if (isBusinessConfigRelationEmpty(b)){
                return true;
            }else{
                return false;
            }
        }else{
            if (isBusinessConfigRelationEmpty(b)){
                return false;
            }else{
                return ((a.defaultValue == b.defaultValue) && listIntegerEquals(a.list, b.list));
            }
        }
    }

    public static boolean businessConfigProfessionEquals(BusinessConfig.ProposalOtherInsured.Profession a, BusinessConfig.ProposalOtherInsured.Profession b){
        if (isBusinessConfigProfessionEmpty(a)){
            if (isBusinessConfigProfessionEmpty(b)){
                return true;
            }else{
                return false;
            }
        }else{
            if (isBusinessConfigProfessionEmpty(b)){
                return false;
            }else{
                return ((a.defaultValue == b.defaultValue) && listIntegerEquals(a.list, b.list));
            }
        }
    }

    public static boolean listIntegerEquals(List<Integer> a, List<Integer> b){
        if (a == null || a.size() == 0){
            if (b == null || b.size() == 0){
                return true;
            }else{
                return false;
            }
        }else{
            if (b == null || b.size() == 0){
                return false;
            }else{
                if (a.size() != b.size()){
                    return false;
                }else{
                    int size = a.size();
                    for (int i = 0; i < size; i++){
                        if (integerEquals(a.get(i), b.get(i))){
                            continue;
                        }else{
                            return false;
                        }
                    }
                    return true;
                }
            }
        }
    }

    public static boolean integerEquals(Integer a, Integer b){
        if (a == null || a == 0){
            if (b == null || b == 0){
                return true;
            }else{
                return false;
            }
        }else{
            if (b == null || b == 0){
                return false;
            }else{
                return a == b;
            }
        }
    }


    public static boolean listStringEquals(List<String> a, List<String> b){
        if (a == null || a.size() == 0){
            if (b == null || b.size() == 0){
                return true;
            }else{
                return false;
            }
        }else{
            if (b == null || b.size() == 0){
                return false;
            }else{
                if (a.size() != b.size()){
                    return false;
                }else{
                    int size = a.size();
                    for (int i = 0; i < size; i++){
                        if (stringEquals(a.get(i), b.get(i))){
                            continue;
                        }else{
                            return false;
                        }
                    }
                    return true;
                }
            }
        }
    }

    public static boolean isProposalOtherInsuredEmpty(BusinessConfig.ProposalOtherInsured a){
        if (a == null) {
            return true;
        }else{
            if (isBusinessConfigAgeEmpty(a.age)
                    && isBusinessConfigProfessionEmpty(a.profession)
                    && isBusinessConfigRelationEmpty(a.relation)
                    && isBusinessConfigSexEmpty(a.sex)){
                return true;
            }else{
                return false;
            }
        }
    }

    public static boolean isBusinessConfigRelationEmpty(BusinessConfig.ProposalOtherInsured.Relation a){
        if (a == null){
            return true;
        }else{
            if (isListEmpty(a.list) && a.defaultValue == 0){
                return true;
            }else{
                return false;
            }
        }
    }

    public static boolean isBusinessConfigAgeEmpty(BusinessConfig.ProposalOtherInsured.Age a){
        if (a == null){
            return true;
        }else{
            if (isListEmpty(a.range) && isTextEmpty(a.defaultValue)){
                return true;
            }else{
                return false;
            }
        }
    }

    public static boolean isBusinessConfigSexEmpty(BusinessConfig.ProposalOtherInsured.Sex a){
        if (a == null){
            return true;
        }else{
            if (isListEmpty(a.list) && a.defaultValue == 0){
                return true;
            }else{
                return false;
            }
        }
    }

    public static boolean isBusinessConfigProfessionEmpty(BusinessConfig.ProposalOtherInsured.Profession a){
        if (a == null){
            return true;
        }else{
            if (isListEmpty(a.list) && a.defaultValue == 0){
                return true;
            }else{
                return false;
            }
        }
    }

    public static boolean isListEmpty(List a){
        if (a == null){
            return true;
        }else{
            if (a.size() == 0){
                return true;
            }else{
                return false;
            }
        }
    }

}
